<template>
  <PageWrapper contentFullHeight>
    <CollapseContainer title="Select基础用例">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset" />
    </CollapseContainer>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicForm, FormSchema, useForm } from '@/components/Form';
  import { CollapseContainer } from '@/components/Container';
  import { PageWrapper } from '@/components/Page';

  import { useMessage } from '@/hooks/web/useMessage';

  const { createMessage } = useMessage();

  // prettierrc-ignore
  let options: any[] = [
    { label: '可选A', value: '1' },
    { label: '必选B', value: '2' },
    { label: '可选C', value: '3' },
    { label: '选项D', value: '4' },
    { label: '可选E', value: '5' },
    { label: '选项F', value: '6' },
    { label: '可选G', value: '7' },
  ];

  // 字段信息
  // prettierrc-ignore
  const schemas: FormSchema[] = [
    {
      field: 'f1',
      component: 'Select',
      label: '单选-静态',
      colProps: { span: 8 },
      defaultValue: '1',
      componentProps: {
        options: options,
      },
    },
    {
      field: 'f2',
      component: 'Select',
      label: '多选-关闭过滤',
      colProps: {
        span: 8,
      },
      defaultValue: ['1', '3'],
      componentProps: {
        options: options,
        mode: 'multiple',
        filterOption: false, // 关闭搜索. Select 过滤只能通过 value 过滤，不能通过 label 过滤
      },
    },
    {
      field: 'f3',
      component: 'Select',
      label: '最多展示3个',
      colProps: {
        span: 8,
      },
      defaultValue: ['1', '2', '3', '4'],
      componentProps: {
        options: options,
        mode: 'multiple',
        filterOption: false,
        maxTagCount: 3,
      },
    },
    {
      field: 'f4',
      component: 'Select',
      label: '根据label过滤',
      colProps: {
        span: 8,
      },
      helpMessage: '超过多个会出现省略号, 关闭过滤',
      defaultValue: ['1'],
      componentProps: {
        options: options,
        mode: 'multiple',
        filterOption: (input, option) => option?.label.toLowerCase().indexOf(input.toLowerCase()) >= 0,
      },
    },
    {
      field: 'f5',
      component: 'Select',
      label: '任意输入',
      colProps: {
        span: 8,
      },
      defaultValue: ['1'],
      componentProps: {
        options: options,
        mode: 'tags',
      },
    },
  ];

  const [register] = useForm({
    labelWidth: 120,
    schemas,
    actionColOptions: { span: 24 },
    fieldMapToTime: [['fieldTime', ['startTime', 'endTime'], 'YYYY-MM']],
  });

  const keyword = ref<string>('');

  // 重置方法
  function handleReset() {
    keyword.value = '';
  }

  // 提交方法
  function handleSubmit(values: any) {
    console.log('values', values);
    createMessage.success('click search,values:' + JSON.stringify(values));
  }
</script>
